function Header(){
	this.init();
}
//html模板
Header.NavTemplate=`<nav class="navbar navbar-inverse container-fluid">
		  <div class="container-fluid">
		    <!-- Brand and toggle get grouped for better mobile display -->
		    <div class="navbar-header">
		      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
		        <span class="sr-only">Toggle navigation</span>
		        <span class="icon-bar"></span>
		        <span class="icon-bar"></span>
		        <span class="icon-bar"></span>
		      </button>
		      <a class="navbar-brand" href="">超市账单管理系统</a>
		    </div>
		
		    <!-- Collect the nav links, forms, and other content for toggling -->
		    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">		     
		      <ul class="nav navbar-nav navbar-right">

		        <li data-toggle="modal" data-target="#loginModal"><a href="/html/login.html">登录</a></li>
		        

		        
		       

		      </ul>
		      <ul class="nav navbar-nav navbar-right hidden login-success">
		        <li><a href="#">欢迎:</a></li>
		        <li><a href="javascript:void(0);" class="link-logout">注销</a></li>
		      </ul>
		    </div><!-- /.navbar-collapse -->
		  </div><!-- /.container-fluid -->
		</nav>`;
$.extend(Header.prototype,{
	init(){
	this.createDom();
	this.loadUser();
	this.addListener();
	},
	//创建DOM
	createDom(){
		$("header").html(Header.NavTemplate);
	},
	//加载登陆成功的用户信息
	loadUser(){
		//从sessionStorage中读取登陆成功的用户信息
		const user = sessionStorage.username;
		if(user){
			//存在，说明有登陆信息
			$(".login-success").removeClass("hidden").prev("ul").remove();
			$(".login-success a:first").html("欢迎："+user);
		}else{//不存在，加载模态框
			//this.createModal();
			//location.reload("/html/login.html");
			window.location.href='/html/login.html';
		}
	},
	//创建登陆与注册模态框
	// createModal(){
	// 	new LoginModal();
	// 	new RegisterModal();
	// },

	//注册事件监听
	addListener(){
		$(".link-logout").on("click",this.logoutHandler);
	},
	//注销事件
	logoutHandler(){
		sessionStorage.removeItem("username");
		$.getJSON("/api/users/logout",(data)=>{
			if(data.res_body.status===1){
				//location.reload();
				window.location.href='/html/login.html';
			}
		})
	}
});
new Header();

// <li data-toggle="modal" data-target="#loginModal"><a href="#">登录</a></li>
// <li data-toggle="modal" data-target="#regModal"><a href="javascript:void(0);" ></a></li>
